package util;

import java.util.HashMap;

/**
 * Ensure that every element has a unique name based on their current name
 * 
 * The goal of this class is to encapsulate an algorithm which will give
 * an incoming Named object a unique name provided that it has been seen 
 * before.  The reason for this class is so that objects which are given
 * generic "type" names can be given real unique names programmatically.
 * 
 * @version 1.0
 * @author Alexander Schearer <aas11@duke.edu>
 */
public class NameSpacer {

	private HashMap<String, Integer> myHistory;

	private static NameSpacer singleton;

	public NameSpacer() {
		myHistory = new HashMap<String, Integer>();
		singleton = this;
	}

	public String getName(String typename) {
		Integer suffix = myHistory.get(typename);
		if (suffix == null)
			suffix = -1;
		suffix++;
		myHistory.put(typename, suffix);
		return typename + suffix;
	}

	public static NameSpacer getSingleton() {
		if (singleton == null)
			singleton = new NameSpacer();
		return singleton;
	}
	
	public static void main(String[] args) {
		NameSpacer namer = NameSpacer.getSingleton();
		System.out.println(namer.getName("block"));
	}
}
